package com.hzy.so.controller;

import com.hzy.common.entities.CommonResult;
import com.hzy.sample.entities.Payment;
import com.hzy.so.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author hzy
 * @since 2022/3/3 22:19
 */
@RestController
public class OrderController {

    public static final String service_payment_addr = "http://sentinel-openfeign-payment/";
    @Autowired
    private RestTemplate restTemplate;

    // http://localhost:8201/load/1
    @RequestMapping("/load/{id}")
    public CommonResult<Payment> fallback(@PathVariable("id") String id){
        CommonResult<Payment> result = restTemplate.getForObject(service_payment_addr + "payment/load/" + id, CommonResult.class);

        if ("4".equals(id)){
            throw new IllegalArgumentException("异常的请求参数:" + id);
        }

        return result;
    }


    // openfeign
    @Autowired
    private PaymentService paymentService;

    // http://localhost:8201/loadByOpenFeign/1
    @RequestMapping("/loadByOpenFeign/{id}")
    public CommonResult<Payment> loadByOpenFeign(@PathVariable("id") String id){
        return paymentService.load(id);
    }

}
